import time
import os
import psutil

class FilePaginator:
    def __init__(self, file_path: str, page_size: int = 100):
        self.file_path = file_path
        self.page_size = page_size
        self.total_pages = self.get_total_pages()

    def read_file_by_line(self):
        """Generator function to read file line by line."""
        with open(self.file_path, 'r') as file:
            for line in file:
                yield line.rstrip()

    def get_page(self, page_num: int):
        """Get a specific page from the file."""
        lines = []
        total_pages = 0
        current_page = 1
        line_count = 0

        for line in self.read_file_by_line():
            # 跳过非目标页
            if line_count < (current_page - 1) * self.page_size:
                line_count += 1
                continue
            
            # 将目标页的line保存下来
            if len(lines) < self.page_size:
                lines.append(line)
            # 满一页，current_page+=1
            if len(lines) == self.page_size:
                # 返回目标页
                if current_page == page_num:
                    return {
                        "line": lines,
                        "page_num": current_page,
                        "page_size": self.page_size,
                        "total_pages": self.total_pages
                    }
                current_page += 1
                lines = []

            line_count += 1

        # 如果目标页不存在，返回空列表
        return {
            "line": [],
            "page_num": page_num,
            "page_size": self.page_size,
            "total_pages": self.total_pages
        }

    def get_total_pages(self):
        """Get the total number of pages in the file."""
        line_count = sum(1 for _ in self.read_file_by_line())
        total_pages = (line_count + self.page_size - 1) // self.page_size
        return total_pages

    def get_line(self, target_line_num: int):
        """Get a specific line from the file by its number."""
        page_num = (target_line_num - 1) // self.page_size + 1
        page = self.get_page(page_num)
        line_index = (target_line_num - 1) % self.page_size
        if 0 <= line_index < len(page["line"]):
            return page["line"][line_index]
        return None

def print_memory_usage(message):
    process = psutil.Process(os.getpid())
    mem_info = process.memory_info()
    print(f"{message}: RSS={mem_info.rss / 1024 / 1024:.2f} MB, VMS={mem_info.vms / 1024 / 1024:.2f} MB")

# 示例代码，用于查找第1551637行
file_path = 'bigfile.log'
line_number = 1551637

paginator = FilePaginator(file_path, page_size=1000)

start_time = time.time()
print_memory_usage("Before fetching the line")
# line_content = paginator.get_line(line_number)
page = paginator.get_page(98)
total_pages = page["total_pages"]
# total_pages = paginator.get_total_pages()
end_time = time.time()
print_memory_usage("After fetching the line")

print(f"page: {total_pages}")
# print(f"Line {line_number}: {line_content}")
# print(f"Total Pages: {total_pages}")
print(f"Time taken: {end_time - start_time} seconds")



def test_file_paginator():
    paginator = FilePaginator('bigfile.log', page_size=100)

    # 测试总页数
    total_pages = paginator.get_total_pages()
    print(f"Total Pages: {total_pages}")

    # 测试获取第一页
    page1 = paginator.get_page(1)
    print(f"Page 1: {page1}")

    # 测试获取最后一页
    last_page = paginator.get_page(total_pages)
    print(f"Last Page: {last_page}")

    # 测试获取指定行
    specific_line = paginator.get_line(1551637)
    print(f"Line 1551637: {specific_line}")

# test_file_paginator()

